<?php
function __autoload($class_name) 
    {
        include $class_name . '.php';
    }
    $page = new page('forum');
    $content = '';
    if(!isset($_GET['id_article']))
        header('location:index.php');
    else
        $id_article = $_GET['id_article'];
    if(isset($_GET['page']))
        $page_num = $_GET['page'];
    else
        $page_num = 1;
     if(isset($_SESSION['user']) && $_SESSION['user']->validate())
     {
         if(isset($_POST['loadPost']))
             {
                 //vyplneny formular na pridani prispevku
                 if(isset($_POST['header']))
                 {
                    $header = $_POST['header'];
                 }
                 if(isset($_POST['text']))
                 {
                    $text = $_POST['text'];
                 }
                 //vlozeni prispevku do databaze
                 $query = "INSERT INTO forums VALUES(
                     null,
                     '{$id_article}',
                     CURDATE(),
                     '{$_SESSION['user']->getId()}',
                     '{$header}',
                     '{$text}',
                     1)";
                  $conn = new dbConnection();
            $sql_result = $conn->db_query($query);  
            if (!$sql_result) 
    {
        die('Invalid query: ' . mysql_error());
    }
             }
     }
    $query = 'SELECT COUNT(id) AS row_count FROM forums WHERE state = true AND id_article = '.$id_article;
    $conn1 = new dbconnection();
    $sql_result = $conn1->db_query($query);
    if (!$sql_result) 
    {
        die('Invalid query: ' . mysql_error());
    }
    $row = mysql_fetch_array($sql_result,MYSQL_ASSOC);
    $row_count = (int)$row['row_count'];
    $query = 'SELECT id,
        id_article,
        date,
        id_user,
        subject,
        text
        FROM forums WHERE state = true AND id_article = '.$id_article;
    $sql_result = $conn1->db_query($query);
    if (!$sql_result) 
    {
        die('Invalid query: ' . mysql_error());
    }
    while($row = mysql_fetch_array($sql_result,MYSQL_ASSOC))
    {        
        $forumPost=new forumPost($row['id'],
        $row['id_article'],        
        $row['date'],
        $row['id_user'],
        $row['subject'],
        $row['text']);
        $content .= $forumPost->render();
    }      
        
    if(isset($_SESSION['user']) && $_SESSION['user']->validate())
    {
        //zadavani noveho prispevku
        $loadPost = new form('loadPost', $_SERVER['REQUEST_URI'], 'post', 'nacist prispevek do dikuze:',
            array(
                new formTextArea('header', 'Nadpis: '),
                new formTextArea('text', 'Text prispevku: '),
                new formSubmit('Nahrat')
                ));
        $content .= $loadPost->render();
    }
    else
    {
        $content .= 'Pro vlozeni prispevku se <a href="login.php">prihlaste</a><br />';
    }
    $paging = new paging( $row_count, $page_num, 5);//error nemuze bejt php_self
    $content .= $paging->render();
    
    echo $page->render($content);

?>
